package com.five.mobile.controller;

import com.five.admin.threadLocal.UserThreadLocal;
import com.five.core.result.Result;
import com.five.core.result.ResultEnum;
import com.five.mobile.pojo.CarInfo;
import com.five.mobile.pojo.CarInfoDetails;
import com.five.mobile.service.CarService;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/car")
public class CarController {

    @Resource
    private CarService carService;
    
    @PostMapping("/addCarInfo")
    public Result addCarInfo(@RequestBody CarInfo carInfo){
        Long userId = UserThreadLocal.get().getId();
        boolean b = carService.addCarInfo(userId, carInfo);
        return b ? Result.buildSuccess() : Result.buildFail(ResultEnum.ADD_FALSE);
    }
    
    @GetMapping("/getCarInfo")
    public Result<List<CarInfo>> getCarInfo() {
        Long userId = UserThreadLocal.get().getId();
        List<CarInfo> carInfo = carService.getCarInfo(userId);
        return Result.buildSuccess(carInfo);
    }
    
    @GetMapping("/getCarInfoDetails")
    public Result<CarInfoDetails> getCarInfoDetails(String carId) {
        Long userId = UserThreadLocal.get().getId();
        CarInfoDetails carInfoDetails = carService.getCarInfoDetails(userId, carId);
        return Result.buildSuccess(carInfoDetails);
    }
    
    @PutMapping("/updateCarInfo")
    public Result updateCarInfo(@RequestBody CarInfo carInfo) {
        Long userId = UserThreadLocal.get().getId();
        carService.updateCarInfo(userId, carInfo);
        return Result.buildSuccess();
    }
    
    @DeleteMapping("/deleteCarInfo")
    public Result<Boolean> deleteCarInfo(String carId) {
        Long userId = UserThreadLocal.get().getId();
        boolean b = carService.deleteCarInfo(userId, carId);
        return b ? Result.buildSuccess() : Result.buildFail(ResultEnum.DELETE_FALSE);
    }
}
